-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for matching internal middlewares #79
Draft
marcospassos
wants to merge
15
commits into
master
Choose a base branch
from
middleware-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
marcospassos
added
bc-break
This change is not backward-compatible
feature
New feature
labels
Oct 13, 2024
commit: |
marcospassos
force-pushed
the
middleware-improvements
branch
from
October 23, 2024 19:13
07deb71
to
1d77e06
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
withCroct
to allow easier handling of both custom and Croct middlewares.@internal
to prevent unintended usage.Improvements
When you're working on a project that already has middleware and want to add Croct's middleware, things can get tricky—especially if there's a matcher in place to determine which routes the middleware should handle.
For example, let's say you have the following middleware setup:
Since Croct's middleware needs to run on all pages (not all paths), you have to refactor this to handle the matching logic inside the middleware itself. But refactoring like this can get complicated, especially if your matcher is complex.
For the example above, you would need to refactor the middleware like this:
While this works, the more complex the matcher, the more challenging this refactoring becomes.
New Solution
This PR introduces a new option to the
withCroct
function that lets you pass in the existing matcher directly. This way, you can make sure your existing middleware only runs when its matcher conditions are met, while Croct's middleware still runs across all pages without requiring complicated refactoring.Here's how you can simplify the previous example with this update:
Header Forwarding Fix
Another key improvement: you no longer need to manually forward headers to the next middleware. Before this, if you forgot to forward headers, you'd lose the ones set by Croct's middleware, which was a common issue when refactoring existing middleware.
This PR solves that by automatically wrapping the
NextResponse.next
function to ensure that all headers are forwarded correctly, preventing these issues.With these changes, the example can be further simplified:
Internal Function Marking
Lastly, to clean up the library, all internal functions are now marked as
@internal
, making it clear that they shouldn’t be used outside of the library itself.Checklist